package com.hospital.schedule.module.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.dto.schedule.module.FindDoctorByDoctorScheduleDto;
import com.hospital.common.pojo.TUser;
import com.hospital.common.utils.TimeUtils;
import com.hospital.common.vo.schedule.module.FindDoctorByDoctorScheduleVo;
import com.hospital.schedule.module.mapper.TUserMapper;
import com.hospital.schedule.module.service.ITDoctorInfoService;
import com.hospital.schedule.module.service.ITDoctorScheduleService;
import com.hospital.schedule.module.service.ITUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-17
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService {

    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private ITDoctorScheduleService tDoctorScheduleService;
    @Autowired
    private ITDoctorInfoService tDoctorInfoService;

    @Override
    public List<FindDoctorByDoctorScheduleVo> findDoctorByDoctorSchedule(FindDoctorByDoctorScheduleDto findDoctorByDoctorScheduleDto) {
        List<String> sevenDaysByStartTime = TimeUtils.findSevenDaysByStartTime(findDoctorByDoctorScheduleDto.getStartTime());
        List<FindDoctorByDoctorScheduleVo> doctorLists = new ArrayList<>();
        for (String day : sevenDaysByStartTime) {
            String startTime = TimeUtils.formatToStartOfDay(day);
            String endTime = TimeUtils.formatToEndOfDay(day);
            List<Long > doctorIdList = tDoctorScheduleService.fineDoctorByDoctorSchedule(findDoctorByDoctorScheduleDto.getDepartmentId(),startTime,endTime);
            System.out.println("doctorIdList = " + doctorIdList);
            List<TUser> doctorList = new ArrayList<>();
            System.out.println("doctorList = " + doctorList);
            if(doctorIdList!=null && doctorIdList.size()>0){
                 doctorList = tUserMapper.findDoctorId(doctorIdList);
            }
            FindDoctorByDoctorScheduleVo findDoctorByDoctorScheduleVo =
                    new FindDoctorByDoctorScheduleVo();
            findDoctorByDoctorScheduleVo.setFindDate(day);
            findDoctorByDoctorScheduleVo.setDoctorList(doctorList);
            doctorLists.add(findDoctorByDoctorScheduleVo);
        }
        return doctorLists;
    }

    @Override
    public String getDoctorNameById(Long doctorId) {
        return tUserMapper.getDoctorNameById(doctorId);
    }

    @Override
    public List<TUser> findAllDoctor(Long departmentId) {
        List<Long > doctorInfoIdList = tDoctorInfoService.findDoctorInfoIdByDepartmentId(departmentId);
        return tUserMapper.findAllDoctor(doctorInfoIdList);
    }

    @Override
    public Long findDoctorDepartmentByUserId(Long doctorId) {
        return tUserMapper.findDoctorDepartmentByUserId(doctorId);
    }

    @Override
    public List<TUser> findDoctorByDepartmentId(Long departmentId) {
        return tUserMapper.findDoctorByDepartmentId(departmentId);
    }

    @Override
    public List<TUser> findDoctorInfoDepartmentByUserId(Long doctorId) {
        Long doctorDepartmentByUserId = this.findDoctorDepartmentByUserId(doctorId);
        List<TUser> doctorByDepartmentId = this.findDoctorByDepartmentId(doctorDepartmentByUserId);
        return doctorByDepartmentId;
    }

    @Override
    public List<TUser> findAllDoctorInfoDepartmentByUserId(Long doctorId) {
        Long doctorDepartmentByUserId = this.findDoctorDepartmentByUserId(doctorId);
        List<TUser> doctorByDepartmentId = this.findDoctorByDepartmentId(doctorDepartmentByUserId);
        return doctorByDepartmentId;
    }

    @Override
    public String findDoctorNameById(Long doctorId) {
        return tUserMapper.findDoctorNameById(doctorId);
    }
}
